package com.example.service;

import com.example.dao.NxScoreInfoDao;
import com.example.entity.Account;
import com.example.entity.NxScoreInfo;
import com.example.vo.NxScoreInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class NxScoreInfoService {

    @Resource
    private NxScoreInfoDao nxScoreInfoDao;

    public NxScoreInfo add(NxScoreInfo nxScoreInfo, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Account user = (Account) session.getAttribute("user");
        nxScoreInfo.setUserId(user.getId());
        nxScoreInfo.setLevel(user.getLevel());
        nxScoreInfo.setUsername(user.getName());
        nxScoreInfoDao.insertSelective(nxScoreInfo);
        return nxScoreInfo;
    }

    public void delete(Long id) {
        nxScoreInfoDao.deleteByPrimaryKey(id);
    }

    public void update(NxScoreInfo nxScoreInfo) {
        nxScoreInfoDao.updateByPrimaryKeySelective(nxScoreInfo);
    }

    public NxScoreInfo findById(Long id) {
        return nxScoreInfoDao.selectByPrimaryKey(id);
    }

    public List<NxScoreInfoVo> findAll() {
        return nxScoreInfoDao.findByName("all");
    }

    public PageInfo<NxScoreInfoVo> findPage(String name, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
        List<NxScoreInfoVo> all = nxScoreInfoDao.findByName(name);
        return PageInfo.of(all);
    }

    public PageInfo<NxScoreInfoVo> findUserPage(String name, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Account user = (Account) session.getAttribute("user");
        PageHelper.startPage(pageNum, pageSize);
        List<NxScoreInfoVo> all = nxScoreInfoDao.findPage(name, user.getId(), user.getLevel());
        return PageInfo.of(all);
    }

    public NxScoreInfo findByCode(String code, Long userId, Integer level) {
        return nxScoreInfoDao.findByCode(code, userId, level);
    }
}
